package com.zys.background.gateway.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zys.background.common.type.SuppressWarningsType;
import com.zys.background.common.entity.Result;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @apiNote 网关统一异常处理
 * @author zys
 * @since 2022-11-25
 */
@Slf4j
@Order(-1)
@Component
public class GatewayHandler implements ErrorWebExceptionHandler {
  @Override
  @SneakyThrows
  @SuppressWarnings(SuppressWarningsType.NULLABLE_PROBLEMS)
  public Mono<Void> handle(ServerWebExchange exchange,  Throwable ex) {
    ServerHttpResponse response = exchange.getResponse();

    if (exchange.getResponse().isCommitted()) {
      return Mono.error(ex);
    }

    String msg;

    if (ex instanceof NotFoundException) {
      msg = "服务未找到";
    } else if (ex instanceof ResponseStatusException) {
      msg = ((ResponseStatusException) ex).getMessage();
    } else {
      msg = "内部服务器错误";
    }

    log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
    response.setStatusCode(HttpStatus.OK);
    response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    DataBuffer dataBuffer = response.bufferFactory().wrap(new ObjectMapper().writeValueAsBytes(Result.fail(msg)));
    return response.writeWith(Mono.just(dataBuffer));
  }
}
